package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Member;
import com.itheima.domain.store.Orders;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SuppressWarnings("ALL")
@WebServlet("/store/orders")
public class OrdersServlet extends BaseServlet {
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int page = 1;
        //int pageSize = 5;
        int size = 5;
       /* if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }*/
        if(StringUtils.isNotBlank(request.getParameter("currentPage"))){
            page = Integer.parseInt(request.getParameter("currentPage"));
        }
        if(StringUtils.isNotBlank(request.getParameter("pageSize"))){
            size = Integer.parseInt(request.getParameter("pageSize"));
        }

        PageInfo all = ordersService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page", all);
        //跳转页面
        request.getRequestDispatcher("/pages/store/orders/orders-list.jsp").forward(request, response);
    }



    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Orders orders = BeanUtil.fillBean(request, Orders.class);
        //调用业务层接口save
        ordersService.delete(orders);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/store/orders?operation=list");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = request.getParameter("id");
        Orders orders = ordersService.findById(id);
        //将数据加载到指定区域，供页面获取
        request.setAttribute("orders", orders);

        String memberId = request.getParameter("memberId");

        Member member = memberService.findById(memberId);
        request.setAttribute("member", member);
        //跳转页面
        request.getRequestDispatcher("/pages/store/orders/orders-show.jsp").forward(request, response);
    }

    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要删除的元素id数组
        //List<String> list = new ArrayList<>();
        String ids = req.getParameter("ids");
        //System.out.println(ids);
        String[] str = ids.split(",");
        for (int i = 0; i < str.length; i++) {
           // list.add(str[i]);
          //  System.out.println(str[i]);
           ordersService.deleteSelected(str[i]);
        }




        //调用service里的deleteSelected方法
        //重定向查询所有 SET FOREIGN_KEY_CHECKS=0
        //      SET FOREIGN_KEY_CHECKS=1
        resp.sendRedirect(req.getContextPath() + "/store/orders?operation=list");
    }

    public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int page=1;
        int size=5;
        if(StringUtils.isNotBlank(request.getParameter("currentPage"))){
            page = Integer.parseInt(request.getParameter("currentPage"));
        }
        if(StringUtils.isNotBlank(request.getParameter("pageSize"))){
            size = Integer.parseInt(request.getParameter("pageSize"));
        }
        String sorting_desc = request.getParameter("sorting_desc");
        System.out.println("sorting_desc = " + sorting_desc);

        PageInfo all = ordersService.search(sorting_desc, page, size);
        request.setAttribute("page", all);
        //请求转发
        request.getRequestDispatcher("/pages/store/orders/orders-list.jsp").forward(request, response);
    }
}
